local extension = Package:new("zyg_am_cards", Package.CardPack)
extension.extensionName = "zzz_zyg_cards"
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["zyg_am_cards"] = "AM扩展",
}

local refreshCards = {
  "zyg_am__kingslayers"
}
local globalCardsRefresh = fk.CreateSkill{
  name = "globalCardsRefresh",
  global = true,
  refresh_events = {fk.GameStart, fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    if event == fk.AfterCardsMove then
      if player ~= player.room.current then return false end
      local cids = {}
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local c = Fk:getCardById(info.cardId)
            if table.contains(refreshCards, c.name) then
              table.insert(cids, info.cardId)
            end
          end
        end
      end
      if #cids > 0 then
        for _, id in ipairs(cids) do
          player.room:moveCards {
            ids = {id},
            toArea = Card.DrawPile,
            moveReason = fk.ReasonJustMove,
            skillName = self.name,
            proposer = nil,
            moveVisible = true,
            drawPilePosition = math.random(1, #player.room.draw_pile)
          }
          player.room:sendLog{
            type = "#GlobalCardsRefresh",
            card = {id}
          }
        end
      end
    else
      return true
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "OriginHandCards", player:getCardIds("h"))
  end
}
Fk:addSkill(globalCardsRefresh)
Fk:loadTranslationTable{
  ["globalCardsRefresh"] = "洗入",
  ["#GlobalCardsRefresh"] = "%card 被洗入牌堆",
}

local decree_skills = {
  "ol_ex__jijiang", "hujia", "jiuyuan", "songwei", "ol_ex__zhiba", "ol_ex__huangtian", "ol_ex__baonue", "ol_ex__xueyi", "ol__lijun", "xingshuai", "qinwang", "zhaofu", "thunder__weidi", "huaibi",
  "shishouy", "ruilue", "chengwu", 
  "mou__jijiang", "mou__jiuyuan", "mou__hujia", "mou__xueyi", "zhonggu", "haoshou", 
  "ty_ex__qinwang", "chongwang", "zhushi",
  "os__juxiang", "os_ex__qiushou", "os_ex__polu",
  "weizhui", "jiemeng", "tongjue", "julian", 
  "mod__guiming", "est__juntu", 
  }
local getDecreeSkills = function(room)
    local mark = room:getTag("DecreeSkills")
    if mark then
      return mark
    else
      local all_skills = {}
      for _, g in ipairs(room.general_pile) do
        for _, s in ipairs(Fk.generals[g].skills) do
          if s.lordSkill then
            table.insert(all_skills, s.name)
          end
        end
      end
      local skills = table.filter(decree_skills, function(s) return table.contains(all_skills, s) end)
      room:setTag("DecreeSkills", skills)
      return skills
    end
  end
local decreeSkill_maxcards = fk.CreateSkill{
    name = "#zyg__decreeSkill_maxcards",
    attached_equip = "zyg__decree",
    correct_func = function(self, player)
        if player:hasSkill(self) then
            local kingdoms = {}
            for _, p in ipairs(Fk:currentRoom().alive_players) do
                table.insertIfNeed(kingdoms, p.kingdom)
            end
            return #kingdoms
        else
            return 0
        end
    end
}
local decreeSkill_choose = fk.CreateSkill{
    name = "zyg__decreeSkill_choose",
    target_num = 0,
    card_num = 0,
    prompt = "decreeSkill_prompt",
    can_use = Util.FalseFunc,
    card_filter = Util.FalseFunc,
    on_use = function(self, room, effect)
        local player = room:getPlayerById(effect.from)
        local skills = table.filter(getDecreeSkills(room), function (skill_name)
            return not player:hasSkill(skill_name, true)
          end)
        if #skills > 0 then
            local choice = room:askForChoice(player, table.random(skills, 5), self.name, "#decree-choice", true)
            room:setPlayerMark(player, "decree_skill", choice)
            room:handleAddLoseSkills(player, choice, nil)
        end
    end
}
Fk:addSkill(decreeSkill_choose)
Fk:addSkill(decreeSkill_maxcards)
local putEquipDW = fk.CreateActiveSkill{
  name = "putEquipDW",
  card_num = 1,
  target_num = 1,
  card_filter = function(self, to_select, selected, targets)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip and Fk:currentRoom():getCardArea(to_select) ~= Player.Equip
  end,
  target_filter = function(self, to_select, selected, cards)
    return #selected == 0 and #cards == 1 and to_select ~= Self.id and
      Fk:currentRoom():getPlayerById(to_select):getEquipment(Fk:getCardById(cards[1]).sub_type) == nil
  end,
  on_use = function(self, room, effect)
    room:moveCards({
      ids = effect.cards,
      from = effect.from,
      to = effect.tos[1],
      toArea = Card.PlayerEquip,
      moveReason = fk.ReasonPut,
    })
    local player = room:getPlayerById(effect.from)
    if not player.dead then
      room:drawCards(player, 2, self.name)
    end
  end
}
Fk:addSkill(putEquipDW)
local decree = fk.CreateTreasure{
  name = "&zyg__decree",
  suit = Card.Heart,
  number = 6,
  equip_skill = decreeSkill_maxcards,
  special_skills = {"putEquipDW"},
  on_install = function(self, room, player)
    Treasure.onInstall(self, room, player) 
    local success = 0
    for _, skill in ipairs(Fk.generals[player.general].skills) do
      if skill.lordSkill then
        player.room:setPlayerMark(player, "decree_skill", skill.name)
        room:handleAddLoseSkills(player, skill.name, nil)
        success = 1
        player.room:handleAddLoseSkills(player, skill.name, nil) 
        break
      end
    end
    if success ~= 1 then
      player.room:askForUseActiveSkill(player, "zyg__decreeSkill_choose", "zyg__decreeSkill_choose", false)
    end
  end,
  on_uninstall = function(self, room, player)
    Treasure.onUninstall(self, room, player)
    if player:getMark("decree_skill") ~= 0 then
      if #player:getMark("decree_skill") > 0 then
          local skill = player:getMark("decree_skill")
          player.room:setPlayerMark(player, "decree_skill", 0)
          player.room:handleAddLoseSkills(player, "-"..skill, nil, false)
      end
    end
  end
}

extension:addCard(decree)
Fk:loadTranslationTable{
    ["zyg__decree"] = "玉玺",
    [":zyg__decree"] = "装备牌·宝物<br/><b>宝物技能</b>："..
    "锁定技，当此牌进入你的装备区时，你视为拥有你武将牌上的主公技（若你的武将牌上没有主公技，则你从随机五个主公技中选择一个获得）"..
    "你的手牌上限+X（X为场上势力数）。置入：你可以将此牌置入其他角色的装备区，然后摸两张牌。",
    ["#zyg__decreeSkill_maxcards"] = "玉玺",
    ["decreeSkill_prompt"] = "玉玺：从五个主公技中选择一个获得",
    ["zyg__decreeSkill_choose"] = "玉玺",
    ["#decree-choice"] = "请选择一个技能获得",
    ["putEquipDW"] = "置入",
    [":putEquipDW"] = "你可以将此牌置入其他角色的装备区，然后摸两张牌",
}
local calabashSkill = fk.CreateTriggerSkill{
  name = "#zyg__calabashSkill",
  anim_type = "special",
  frequency = Skill.Compulsory,
  attached_equip = "zyg__calabash",
  events = {fk.RoundStart, fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.RoundStart and player:hasSkill(self) then
      return not player.dead
    elseif event == fk.EventPhaseEnd and player.phase == Player.Play and target == player then
      return target:getCurrentExtraTurnReason() == "#zyg__calabashSkill"
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      player:gainAnExtraTurn(true, self.name)
    elseif event == fk.EventPhaseEnd then
      local n = 0
      for _, e in ipairs(player.room.logic:getActualDamageEvents(999, function(e) return e.data[1].from == player end)) do
        local damage = e.data[1]
        n = n + damage.damage
      end
      if n > 0 then
        room:setPlayerMark(player, "@@calabash_reward-round", 1)
      else
        room:setPlayerMark(player, "@@calabash_skip-round", 1)
      end
    end
  end,
  refresh_events = {fk.EventPhaseChanging, fk.DrawNCards, fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    if event == fk.EventPhaseChanging then
      return target == player and player:getCurrentExtraTurnReason() == "#zyg__calabashSkill"
    elseif event == fk.DrawNCards and player:getMark("@@calabash_reward-round") > 0 then
      return true
    else
      return target == player and player:getMark("@@calabash_skip-round") > 0
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.EventPhaseChanging then
      local excludePhases = { Player.Start, Player.Judge, Player.Draw, Player.Discard, Player.Finish }
      for _, phase in ipairs(excludePhases) do
        table.removeOne(player.phases, phase)
      end
    elseif event == fk.DrawNCards then
      data.n = data.n + 1
    else
      local room = player.room
      player:skip(Player.Play)
      player:skip(Player.Discard)
      room:setPlayerMark(player, "@@calabash_skip-round", 0)
    end
  end,

}
Fk:addSkill(calabashSkill)
local calabash = fk.CreateTreasure{
  name = "&zyg__calabash",
  suit = Card.Heart,
  number = 1,
  equip_skill = calabashSkill,
}
extension:addCard(calabash)
Fk:loadTranslationTable{
  ["zyg__calabash"] = "争先红葫芦",
  [":zyg__calabash"] = "装备牌·宝物<br/><b>宝物技能</b>："..
  "锁定技，每轮开始时，你执行一个仅有出牌阶段的额外回合，此阶段结束时，若你于此阶段内：造成过伤害，你本轮摸牌阶段摸牌数+1；"..
  "未造成过伤害，你跳过本轮你的下一回合的出牌阶段与弃牌阶段。",
  ["#zyg__calabashSkill"] = "争先红葫芦",
  ["@@calabash_reward-round"] = "额外摸牌",
  ["@@calabash_skip-round"] = "跳过阶段",
}
local moonSpearSkill = fk.CreateTriggerSkill{
  name = "#fhyx_moon_spear_skill",
  attached_equip = "zyg_am__moon_spear",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or player.phase ~= Player.NotActive or player:getMark("_fhyx_moon_spear-turn") ~= 1 then return false end
    return not player.dead and not player:prohibitUse(Fk:cloneCard("slash"))
  end,
  on_cost = function(self, event, target, player, data)
    local use = player.room:askForUseCard(player, "slash", nil, "#fhyx_moon_spear_skill-ask", true)
    if use then
      self.cost_data = use
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setEmotion(player, "./packages/zzz_zyg_cards/image/anim/fhyx_moon_spear")
    room:useCard(self.cost_data)
  end,

  refresh_events = {fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    if player.phase ~= Player.NotActive or player:getMark("_fhyx_moon_spear-turn") > 1 then return false end
    for _, move in ipairs(data) do
      if move.from == player.id then
        return table.find(move.moveInfo, function(info)
          return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
        end)
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_fhyx_moon_spear-turn", 1)
  end,
}
Fk:addSkill(moonSpearSkill)
local moonSpear = fk.CreateWeapon{
  name = "&zyg_am__moon_spear",
  suit = Card.Diamond,
  number = 12,
  attack_range = 3,
  equip_skill = moonSpearSkill,
}

extension:addCard(moonSpear)
Fk:loadTranslationTable{
  ["zyg_am__moon_spear"] = "银月枪",
  [":zyg_am__moon_spear"] = "装备牌·武器<br /><b>攻击范围</b>：３<br /><b>武器技能</b>：一名其他角色回合结束时，若你于此回合失去过手牌，则你可以对攻击范围内的一名本回合获得过手牌的角色使用一张【杀】。",
  ["#fhyx_moon_spear_skill"] = "银月枪",
  ["#fhyx_moon_spear_skill-ask"] = "银月枪：你可以使用一张【杀】",
  [":fhyx_moon_spear_skill"] = "银月枪",
}

local jade_band_maxcards = fk.CreateMaxCardsSkill{
  name = "#jade_band_maxcards",
  attached_equip = "zyg_am__jade_band",
  correct_func = function(self, player)
    return 0
  end
}
Fk:addSkill(jade_band_maxcards)
local jade_band_skill = fk.CreateActiveSkill{
  name = "jade_band_skill&",
  anim_type = "offensive",
  prompt = function ()
    return "#jade_band-invoke:" .. Self:getMark("jade_band_target")[1]
  end,
  target_filter = function ()
    return false
  end,
  card_filter = function ()
    return false
  end,
  can_use = function (self, player, card, extra_data)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and player:getMark("jade_band_target") ~= 0
     and not Fk:currentRoom():getPlayerById(player:getMark("jade_band_target")[1]).dead
     and table.find(Fk:currentRoom():getPlayerById(player:getMark("jade_band_target")[1]):getCardIds("e"), function (id)
      return Fk:getCardById(id).name == "zyg_am__jade_band"
     end)
  end,
  on_use = function (self, room, effect)
    local from = room:getPlayerById(effect.from)
    local to = room:getPlayerById(from:getMark("jade_band_target")[1])
    room:doIndicate(from.id, {to.id})
    local use = room:askForUseCard(to, "slash", "slash", "#jade_band-ask", true)
    if use then
      room:useCard(use)
    end
  end
}
Fk:addSkill(jade_band_skill)
local presentEquipJB = fk.CreateActiveSkill{
  name = "presentEquipJB",
  card_num = 1,
  target_num = 1,
  card_filter = function(self, to_select, selected, targets)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip and not Fk:currentRoom():getCardArea(to_select) == Player.Equip
  end,
  target_filter = function(self, to_select, selected, cards)
    return #selected == 0 and #cards == 1 and to_select ~= Self.id and
      Fk:currentRoom():getPlayerById(to_select):getEquipment(Fk:getCardById(cards[1]).sub_type) == nil
  end,
  on_use = function(self, room, effect)
    room:moveCards({
      ids = effect.cards,
      from = effect.from,
      to = effect.tos[1],
      toArea = Card.PlayerEquip,
      moveReason = fk.ReasonPut,
    })
    room:sendLog{
      type = "#PutJadeBandInto",
      from = effect.from,
      to = effect.tos
    }
    local player = room:getPlayerById(effect.from)
    if not player.dead then
      room:setPlayerMark(player, "jade_band_target", {effect.tos[1]})
      room:handleAddLoseSkills(player, "jade_band_skill&", nil, false)
    end
  end
}
Fk:addSkill(presentEquipJB)
local jade_band = fk.CreateTreasure{
  name = "&zyg_am__jade_band",
  suit = Card.Heart,
  number = 6,
  equip_skill = jade_band_maxcards,
  special_skills = {"presentEquipJB"},
  on_install = function(self, room, player)
    Treasure.onInstall(self, room, player)
  end,
  on_uninstall = function(self, room, player)
    Treasure.onUninstall(self, room, player)
    for _, p in ipairs(player.room.alive_players) do
      if p:getMark("jade_band_target") ~= 0 or p:hasSkill(jade_band_skill, true) then
        player.room:setPlayerMark(p, "jade_band_target", 0)
        player.room:handleAddLoseSkills(p, "-" .. "jade_band_skill&", nil, false)
      end
    end
  end
}
extension:addCard(jade_band)
Fk:loadTranslationTable{
  ["zyg_am__jade_band"] = "玉带",
  [":zyg_am__jade_band"] = "装备牌·宝物<b>宝物技能</b>：当此牌因赠予植入你的装备区后，赠予者出牌阶段限一次，其可以令你选择是否使用一张【杀】。",
  ["jade_band_skill&"] = "玉带",
  [":jade_band_skill&"] = "出牌阶段限一次，你可以令装备着【玉带】的角色选择是否使用一张【杀】。",
  ["#jade_band-invoke"] = "玉带：你可以令 %src 选择是否使用一张【杀】",
  ["#jade_band-ask"] = "玉带：你可以使用一张【杀】",
  ["presentEquipJB"] = "赠予",
  [":presentEquipJB"] = "出牌阶段，你可以将手牌中的【玉带】置入一名其他角色的装备区。",
  ["#PutJadeBandInto"] = "%from 将【玉带】赠予 %to 并置入装备区",
}

local shadow_generator_skill = fk.CreateActiveSkill{
  name = "shadow_generator_skill",
  anim_type = "control",
  attached_equip = "zyg_am__shadow_generator",
  min_target_num = 1,
  max_card_num = 2,
  card_filter = function ()
    return false
  end,
  target_filter = function (self, to_select, selected)
    return to_select ~= Self.id and #selected < 2
  end,
  can_use = function (self, player, card, extra_data)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and table.find(player:getCardIds("e"), function (id)
      return Fk:getCardById(id):getMark("@@shadow_generator_success") > 0
    end)
  end,
  on_use = function (self, room, effect)
    for _, id in ipairs(effect.tos) do
      room:setPlayerMark(room:getPlayerById(id), "@@shadow_generator-turn", 1)
    end
  end,
}
Fk:addSkill(shadow_generator_skill)
local shadow_generator_trigger = fk.CreateTriggerSkill{
  name = "#shadow_generator_trigger",
  anim_type = "special",
  priority = 0.1,
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(shadow_generator_skill) and player.phase == Player.Finish and table.find(player:getCardIds("e"), function (id)
      local c = Fk:getCardById(id)
      return c.name == "zyg_am__shadow_generator" and c:getMark("@@shadow_generator_success") == 0
    end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cids = U.prepareDeriveCards(room, {{"shade", Card.Spade, 1}, {"shade", Card.Spade, 1}}, "shadow_generator_shades")
    local ids = room:askForCard(player, 1, 2, false, self.name, false, ".|.|.|.|.|.|"..table.concat(cids, ","), "#shadow_generator-ask", cids)
    if #ids > 0 then
      local shades = {}
      for i = 1, #ids do
        table.insert(shades, room:printCard("shade", Card.Spade, 1).id)
      end
      if #shades > 0 then
        for _, id in ipairs(shades) do
          room:moveCards {
            ids = {id},
            toArea = Card.DrawPile,
            moveReason = fk.ReasonJustMove,
            skillName = self.name,
            proposer = player.id,
            moveVisible = true,
            drawPilePosition = math.random(1, #room.draw_pile)
          }
        end
        room:sendLog{
          type = "#ShadowGeneratorPutShadesIn",
          from = player.id,
          arg = #shades
        }
        if not player.dead then
          local equipment = table.filter(player:getCardIds("e"), function (id)
            return Fk:getCardById(id).name == "zyg_am__shadow_generator"
          end)
          for _, id in ipairs(equipment) do
            local c = Fk:getCardById(id)
            room:addCardMark(c, "@shadow_generator_count", #shades)
            if c:getMark("@shadow_generator_count") >= 5 then
              room:setCardMark(c, "@shadow_generator_count", 0)
              room:setCardMark(c, "@@shadow_generator_success", 1)
            end
          end
        end
      end
    end
  end
}
Fk:addSkill(shadow_generator_trigger)
shadow_generator_skill:addRelatedSkill(shadow_generator_trigger)
local shadow_generator_prohibit = fk.CreateProhibitSkill{
  name = "#shadow_generator_prohibit",
  global = true,
  prohibit_use = function(self, player, card)
    return player:getMark("@@shadow_generator-turn") ~= 0
  end,
  is_prohibited = function(self, from, to, card)
    return to:getMark("@@shadow_generator-turn") ~= 0
  end,
}
local shadow_generator_tiger_hp = fk.CreateTriggerSkill{
  name = "#shadow_generator_tiger_hp",
  global = true,
  refresh_events = {fk.PreHpRecover, fk.PreHpLost, fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@shadow_generator-turn") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      data.damage = 0
    else
      data.num = 0
    end
    return true
  end,
}
Fk:addSkill(shadow_generator_prohibit)
Fk:addSkill(shadow_generator_tiger_hp)
local shadow_generator = fk.CreateTreasure{
  name = "&zyg_am__shadow_generator",
  suit = Card.Spade,
  number = 13,
  equip_skill = shadow_generator_skill,
}
extension:addCard(shadow_generator)
Fk:loadTranslationTable{
  ["zyg_am__shadow_generator"] = "鬼谷子",
  [":zyg_am__shadow_generator"] = "装备牌·宝物<br/><b>宝物技能</b>：锁定技：结束阶段，你须将一至两张【影】洗入牌堆。当你以此法洗入至少五张【影】后，修改此装备。"..
  "<br/>修改后：出牌阶段限一次，你可以选择至多两名其他角色，令其本回合不能使用或打出牌，不能成为牌的目标，且防止体力变化。",
  ["shadow_generator_skill"] = "鬼谷子",
  [":shadow_generator_skill"] = "锁定技：结束阶段，你须将一至两张【影】洗入牌堆。当你以此法洗入至少五张【影】后，修改此装备。"..
  "<br/><font color=purple>修改后：出牌阶段限一次，你可以选择至多两名其他角色，令其本回合不能使用或打出牌，不能成为牌的目标，且防止体力变化。</font>",
  ["@shadow_generator_count"] = "累计",
  ["@@shadow_generator_success"] = "已修改",
  ["#shadow_generator_trigger"] = "鬼谷子",
  ["@@shadow_generator-turn"] = "鬼谷子",
  ["#shadow_generator-ask"] = "鬼谷子：你须将一至两张【影】洗入牌堆",
  ["#ShadowGeneratorPutShadesIn"] = "%from 将 %arg 张【影】洗入了牌堆",
}

local kingslayers_skill = fk.CreateTriggerSkill{
  name = "#kingslayers_skill",
  anim_type = "special",
  frequency = Skill.Compulsory,
  attached_equip = "zyg_am__kingslayers",
  events = {fk.Damage},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target and data.card and data.card.trueName == "slash"
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = table.map(table.filter(player:getCardIds("e"), function (id)
      return Fk:getCardById(id).name == "zyg_am__kingslayers"
    end), function (id)
      return Fk:getCardById(id)
    end)

    local availableSkills = room:getTag("kingslayersSkills") or {}
    if #availableSkills == 0 then
      for _, general in ipairs(room.general_pile) do
        for _, skillName in ipairs(Fk.generals[general]:getSkillNameList()) do
          local skill = Fk.skills[skillName]
          if table.contains({ Skill.Compulsory, Skill.Frequent, Skill.NotFrequent }, skill.frequency) and
          not skill.lordSkill and
          not skill.isHiddenSkill and
          #skill.attachedKingdom == 0 and
          string.find(Fk:translate(":" .. skill.name, "zh_CN"), "使用【杀】") then
            table.insertIfNeed(availableSkills, skillName)
          end
        end
      end

      room:setTag("kingslayersSkills", availableSkills)
    end

    if #cards > 0 and #availableSkills > 0 then
      for _, c in ipairs(cards) do
        local mark = c:getTableMark("kingslayers_skills")
        availableSkills = table.filter(availableSkills, function (s)
          return not table.contains(mark, s)
        end)
        local skill = table.random(availableSkills)
        table.insertIfNeed(mark, skill)
        room:setCardMark(c, "kingslayers_skills", mark)
        room:sendLog{
          type = "#KingslayersRecorder",
          card = {c.id},
          arg = Fk:translate(skill)
        }
        if not player:hasSkill(Fk.skills[skill], true) then
          room:handleAddLoseSkills(player, skill, nil, true)
        end
      end
    end
  end
}
Fk:addSkill(kingslayers_skill)
local kingslayers = fk.CreateWeapon{
  name = "&zyg_am__kingslayers",
  suit = Card.Spade,
  number = 1,
  attack_range = 2,
  equip_skill = kingslayers_skill,
  on_install = function(self, room, player)
    Treasure.onInstall(self, room, player)
    local skills = self:getTableMark("kingslayers_skills")
    for _, s in ipairs(skills) do
      if not player:hasSkill(Fk.skills[s], true) then
        player.room:handleAddLoseSkills(player, s, nil, true)
      end
    end
  end,
  on_uninstall = function(self, room, player)
    Treasure.onUninstall(self, room, player)
    local skills = self:getTableMark("kingslayers_skills")
    for _, s in ipairs(skills) do
      if player:hasSkill(Fk.skills[s], true, true) then
        player.room:handleAddLoseSkills(player, "-" .. s, nil, true)
      end
    end
  end
}

extension:addCard(kingslayers)
Fk:loadTranslationTable{
  ["zyg_am__kingslayers"] = "弑君",
  [":zyg_am__kingslayers"] = "装备牌·武器<br /><b>攻击范围</b>：３<br /><b>武器技能</b>：锁定技，每回合限一次，你使用【杀】造成伤害后，随机将一个含有“使用【杀】”"..
  "的技能写入本牌。此牌置入弃牌堆时，改为洗回牌堆。",
  ["#kingslayers_skill"] = "弑君",
  ["#KingslayersRecorder"] = "〖%arg〗 被注入 %card",
}
local awakenFromDreamSkill = fk.CreateActiveSkill{
  name = "awaken_from_dream_skill",
  prompt = "#awaken_from_dream_skill",
  target_num = 0,
  mod_target_filter = function(self, to_select, selected, player, card, distance_limited)
    local to = Fk:currentRoom():getPlayerById(to_select)
    return not to:isKongcheng()
  end,
  can_use = function (self, player, card, extra_data)
    return not player:prohibitUse(card) and #table.filter(player:getCardIds("h"), function (id)
      return Fk:getCardById(id) ~= card 
    end) > 0 and player:usedCardTimes("awaken_from_dream") == 0
  end,
  on_use = function(self, room, cardUseEvent)
    if not cardUseEvent.tos or #TargetGroup:getRealTargets(cardUseEvent.tos) == 0 then
      cardUseEvent.tos = { { cardUseEvent.from } }
    end
  end,
  on_effect = function(self, room, effect)
    local to = room:getPlayerById(effect.to)
    if not to.dead then
      to:throwAllCards("h", self.name)
      if not to.dead then
        local cids = to:getTableMark("OriginHandCards")
        if #cids > 0 then
          local cards = {}
          for _, id in ipairs(cids) do
            if table.contains({Card.DiscardPile, Card.DrawPile, Card.Processing, Card.PlayerEquip, Card.PlayerJudge}, room:getCardArea(id))
             or (not table.contains(to:getCardIds("h"), id) and room:getCardArea(id) == Card.PlayerHand) then
              table.insertIfNeed(cards, id)
            end
          end
          if #cards > 0 then
            
            room:moveCardTo(cards, Card.PlayerHand, to, fk.ReasonJustMove, self.name, nil, false, to.id)
          end
        end
      end
    end
  end
}
local awakenFromDream = fk.CreateTrickCard{
  name = "&awaken_from_dream",
  skill = awakenFromDreamSkill,
  is_damage_card = false,
  suit = Card.Heart,
  number = 12,
}
extension:addCard(awakenFromDream)
Fk:loadTranslationTable{
  ["awaken_from_dream"] = "大梦初醒",
  [":awaken_from_dream"] = "锦囊牌<br /><b>时机</b>：出牌阶段<br /><b>目标</b>：你<br /><b>效果</b>：目标角色弃置所有手牌，然后"..
  "获得其所有初始手牌。（每回合限一次）",
  ["awaken_from_dream_skill"] = "大梦初醒",
  [":awaken_from_dream_skill"] = "大梦初醒",
  ["#awaken_from_dream_skill"] = "大梦初醒：弃置所有手牌，然后获得你的初始手牌",
}

local addRandomEquipment = fk.CreateTriggerSkill{
  name = "am__addRandomEquipment",
  global = true,
  refresh_events = {fk.GamePrepared},
  can_refresh = function (self, event, target, player, data)
    return player == player.room.current and not table.contains(player.room.disabled_packs, "zyg_am_cards")
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local cardNames = {
      {"zyg__decree", Card.Heart, 6},
      {"zyg__calabash", Card.Heart, 1},
      {"zyg_am__moon_spear", Card.Diamond, 12},
      {"role__wooden_ox", Card.Diamond, 5},
      {"zyg_am__jade_band", Card.Heart, 6},
      {"zyg_am__shadow_generator", Card.Spade, 13},
      {"zyg_am__kingslayers", Card.Spade, 1},
      {"awaken_from_dream", Card.Heart, 12},
    }
    local addNames = table.random(cardNames, 4)
    if #addNames > 0 then
      local cids = {}
      for _, tb in ipairs(addNames) do
        local c = room:printCard(tb[1], tb[2], tb[3])
        table.insert(cids, c.id)
      end
      if #cids > 0 then
        room:moveCardTo(cids, Card.DrawPile, nil, fk.ReasonJustMove, self.name)
        room:moveCardTo(cids, Card.Processing, player, fk.ReasonJustMove, self.name)
        room:delay(2000)
        local nameText = ""
        for _, id in ipairs(cids) do
          room:moveCards {
            ids = {id},
            toArea = Card.DrawPile,
            moveReason = fk.ReasonJustMove,
            skillName = self.name,
            moveVisible = true,
            drawPilePosition = math.random(1, #room.draw_pile)
          }
          nameText = nameText .. " 【" .. Fk:translate(Fk:getCardById(id).name) .. "】 "
        end
        room:sendLog{
          type = "#RandomAMCards",
          arg = nameText
        }
      end
    end
  end
}
Fk:addSkill(addRandomEquipment)
Fk:loadTranslationTable{
  ["#RandomAMCards"] = "AM拓展中的 %arg 被洗入牌堆",
}

-- 在 moonSpearSkill 相关技能后加上回合开始清零标记
local clearMoonSpearMark = fk.CreateTriggerSkill{
  name = "#clear_fhyx_moon_spear_mark",
  global = true,
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "_fhyx_moon_spear-turn", 0)
  end,
}
Fk:addSkill(clearMoonSpearMark)

return extension